package cn.bsb.controller.rece;
                       
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;

import cn.bsb.pojo.Article;
import cn.bsb.pojo.Bookmark;
import cn.bsb.pojo.Comment;
import cn.bsb.pojo.DevUser;
import cn.bsb.pojo.Recommend;
import cn.bsb.pojo.Thematic;
import cn.bsb.pojo.like;
import cn.bsb.service.rece.ArticleService;
import cn.bsb.service.rece.LoginService;
import cn.bsb.service.rece.RecommendService;
import cn.bsb.service.rece.ThematicService;
import cn.bsb.tools.Constants;
@Controller

public class ArticleControll {
	@Resource(name="ArticleService")
  private ArticleService articleService;
	@Autowired
	private ThematicService thematicService;
	@Autowired
	private LoginService loginService;
	 @Resource(name="RecommendService")
	  private RecommendService recommendService;
	 @RequestMapping("/articlelist")
	  public ModelAndView  articlelist(@RequestParam(name="pageIndex",required=false)String pageIndex, Article article,
			                           @RequestParam(name="thematicid",required=false)String thematicid) {
		  ModelAndView mav=new ModelAndView();
		  String _title=article.getTitle();
		  int pageNum=1;
		  if(!"".equals(pageIndex)&&pageIndex!=null) {
			  pageNum=Integer.parseInt(pageIndex);
		  }
		  if(!"".equals(thematicid)&&thematicid!=null) {
			  article.setThematic_id(Integer.parseInt(thematicid));
		  }
		  //左侧展示推荐文章
		  List<Recommend> list=recommendService.recommendList(6).getList();
		  List value=new ArrayList();
		  for (Recommend recommend : list) {
			  value.add(recommend.getArticle_id());
		  }
		  List<Article> Recommends=articleService.getRecomment(value);
		  //图片展示推荐文章
		  List<Recommend> list2=recommendService.recommendList(4).getList();
		  List value2=new ArrayList();
		  for (Recommend recommend : list2) {
			  value2.add(recommend.getArticle_id());
		  }
		  List<Article> Recommendimg=articleService.getRecomment(value2);
		 //专题展示
		  List<Thematic> thematics=thematicService.getThematic(); 
		  //展示文章
		  PageInfo<Article> page=articleService.getArticles(pageNum, Constants.pageSize,article);
		  mav.addObject("pageinfo",page);
		  mav.addObject("Recommends",Recommends);
		  mav.addObject("Recommendimg",Recommendimg);
		  mav.addObject("thematiclist",thematics);
		  mav.addObject("title", _title);
		  mav.setViewName("rece/index");
		  return mav;
	  }
  @RequestMapping(value="/sys/zhuyeList")
	public String index2(Model model,@RequestParam("id")Integer uid,@RequestParam(value="pageNumber",required=false,defaultValue="1")int pageNum) {
		PageInfo<Article> page = articleService.getList(uid, pageNum, Constants.PAGESIZE);
		List<Article> list = page.getList();
		int count = articleService.getCount(uid);
		DevUser user = loginService.getUser(uid);
		model.addAttribute("page",page);
		model.addAttribute("articleList",list);
		model.addAttribute("user",user);
		model.addAttribute("count",count);
		return "rece/zhuye2";
	}
  	@RequestMapping(value = "/articleView")
	public String getArticleView(HttpSession session, @RequestParam("id") Long id, Model model,
			@ModelAttribute("comment") Comment comment) {
  		System.out.println(id);
  		DevUser duUser=null;
		List<Comment> commentlist = null;
		like like = null;
		Bookmark book = null;
		DevUser devuser = (DevUser) session.getAttribute("loginUser");
		duUser=articleService.getDevuserByAricleId(id);
		model.addAttribute("devuser", duUser);
		if(devuser!=null) {
			 like = articleService.getLike(devuser.getId(), id);
			 model.addAttribute("like", like);
			 	book = articleService.isBookmark(devuser.getId(), id);
				model.addAttribute("book", book);
		}
		
		Article article = articleService.getArticle(id);
		model.addAttribute("articleView", article);
		commentlist = articleService.getComment(id);
		model.addAttribute("commentlst", commentlist);
		articleService.addlook(id.intValue());
		
		return "rece/articleView";
	}
	@RequestMapping("/sys/booklist")
	public String booklist(long id,Model mod) {
		List<Bookmark> booklist = articleService.getBookList(id);
		mod.addAttribute("booklist",booklist);
		System.out.println(booklist.get(0).getTime());
		return "rece/userBook";
	}

	
	@RequestMapping(value="/sys/contact",method=RequestMethod.GET)
	public String getContact(@RequestParam("id")Integer uid,Model model) {
		List<Thematic> list =thematicService.getThematicList();
		model.addAttribute("thematicList",list);
		model.addAttribute("id",uid);
		return "rece/contact2" ;
	}
	
	@RequestMapping(value="/sys/contact",method=RequestMethod.POST)
	public String add(Article ai) {
		ai.setCreattime(new Date());
		ai.setLik(0);
		System.out.println(ai.getThematic_id());
		articleService.save(ai);
		return "redirect:/sys/zhuyeList?id="+ai.getDev_user_id() ;
	}
	
	@RequestMapping(value="update",method=RequestMethod.GET)
	public String update(@RequestParam("id") long id,Model model) {
		List<Thematic> list =thematicService.getThematicList();
		model.addAttribute("thematicList",list);
		Article al =articleService.getArticle(id);
		model.addAttribute("article",al);
		return "rece/articlemodify" ;
	}
	
	
	@RequestMapping(value="update",method=RequestMethod.POST)
	public String updateGo(Article ai) {
			ai.setModificationtime(new Date());
			articleService.update(ai);
		return "redirect:/zhuyeList?id="+ai.getDev_user_id() ;
	}
	
	@RequestMapping(value = "like")
	@ResponseBody
	public Object getLike(@RequestParam(value = "vid") Long vid, @RequestParam(value = "aid") Long aid, Model mod) {
		System.out.println("进入");
		like like = articleService.getLike(vid, aid);
		Map<String, String> map = new HashMap<String, String>();
		if (like == null) {
			int count = articleService.addLike(vid, aid);
			if (count == 1) {
				articleService.like(aid);
				map.put("is", "yes");
			} else {
				map.put("is", "no");
			}

		} else {
			map.put("is", "yes");
		}

		return map;
	}

	@RequestMapping("/comment")
	public String isComment( Comment comment, Model mod) {
		comment.setTime(new Date());
		int count = articleService.isComment(comment);
		if (count == 1) {

			return "redirect:/articleView?id=" + comment.getArticleId();
		}
		return "";
	}

	@RequestMapping("/book")
	@ResponseBody
	public Object isBook(@RequestParam Long vid, @RequestParam Long aid, Bookmark book) {
		Map<String, String> map = new HashMap<String, String>();

		book.setArticle_id(aid.intValue());
		book.setDev_user_id(vid.intValue());
		book.setTime(new Date());
		int count = articleService.addBook(book);
		if(count==1)
		map.put("isbook", "yes");

		// TODO Auto- catch block

		return map;
	}

	@RequestMapping("/nobook")
	@ResponseBody
	public Object noBook(@RequestParam Long vid, @RequestParam Long aid)  {
		Map<String, String> map = new HashMap<String, String>();
		int count = articleService.deBookmark(vid, aid);
		if(count==1)
			map.put("isbook", "yes");

			// TODO Auto- catch block

			return map;

	}

}
